

# 可以单独运行的一个airtest线性脚本

# from airtest.core.api import *
# # import time·
# from airtest.core.error import TargetNotFoundError
#
#
# class AppLoginTester:
#     """应用登录测试器"""
#     def __init__(self, device_uri, package_name, test_account, test_password):
#         """
#         初始化测试器
#         :param device_uri: 设备连接URI (如 "Android://127.0.0.1:5037/emulator-5554")
#         :param package_name: 应用包名
#         :param test_account: 测试账号
#         :param test_password: 测试密码
#         """
#         self.device_uri = device_uri
#         self.package_name = package_name
#         self.test_account = test_account
#         self.test_password = test_password
#         self.init_device()
#
#     def init_device(self):
#         """初始化设备连接"""
#         try:
#             connect_device(self.device_uri)
#             print(f"✅ 成功连接设备: {self.device_uri}")
#         except Exception as e:
#             raise Exception(f"❌ 设备连接失败: {str(e)}")
#
#     def start_application(self):
#         """启动应用"""
#         start_app(self.package_name)
#         time.sleep(5)  # 等待应用启动稳定
#         print(f"✅ 已启动应用: {self.package_name}")
#
#     def stop_application(self):
#         """停止应用"""
#         stop_app(self.package_name)
#         print(f"✅ 已关闭应用: {self.package_name}")
#
#     def click_element(self, template_path, threshold=0.7, timeout=10, action_name="元素"):
#         """
#         点击指定元素
#         :param template_path: 模板图片路径
#         :param threshold: 匹配阈值
#         :param timeout: 超时时间(秒)
#         :param action_name: 元素名称(用于日志)
#         """
#         template = Template(template_path, threshold=threshold)
#         try:
#             if wait(template, timeout=timeout):
#                 touch(template)
#                 time.sleep(2)  # 等待操作生效
#                 print(f"✅ 成功点击: {action_name}")
#                 return True
#             else:
#                 raise TargetNotFoundError(f"未找到 {action_name} (超时{timeout}秒)")
#         except Exception as e:
#             raise Exception(f"❌ 点击{action_name}失败: {str(e)}")
#
#     def input_content(self, template_path, input_text_content, threshold=0.6, action_name="输入框"):
#         """
#         在指定输入框输入文本
#         :param template_path: 输入框模板路径
#         :param input_text_content: 要输入的文本
#         :param threshold: 匹配阈值
#         :param action_name: 输入框名称(用于日志)
#         """
#         # 先点击输入框
#         self.click_element(template_path, threshold=threshold, action_name=action_name)
#         # 输入文本
#         text(input_text_content)
#         time.sleep(1)
#         print(f"✅ 已在{action_name}输入文本")
#
#     def check_and_click_optional(self, template_path, threshold=0.6, action_name="可选元素"):
#         """检查并点击可选元素(如协议勾选框)"""
#         template = Template(template_path, threshold=threshold)
#         if exists(template):
#             touch(template)
#             time.sleep(1)
#             print(f"✅ 已点击可选元素: {action_name}")
#         else:
#             print(f"ℹ️ 未找到可选元素: {action_name}, 跳过操作")
#
#     def verify_login_success(self, success_template, threshold=0.7, timeout=10):
#         """验证登录成功"""
#         template = Template(success_template, threshold=threshold)
#         if wait(template, timeout=timeout):
#             print("✅ 登录验证成功！")
#             return True
#         else:
#             raise TargetNotFoundError("登录成功但未找到验证标识")
#
#     def run_login_test(self,
#                       my_tab_tpl,
#                       phone_input_tpl,
#                       password_input_tpl,
#                       agree_tpl,
#                       login_btn_tpl,
#                       success_tpl):
#         """执行完整登录测试流程"""
#         try:
#             # 启动应用
#             self.start_application()
#
#             # 1. 切换到"我的"页面
#             self.click_element(my_tab_tpl, action_name="我的标签页")
#
#             # 2. 输入手机号
#             self.input_content(phone_input_tpl, self.test_account, action_name="手机号输入框")
#
#             # 3. 输入密码
#             self.input_content(password_input_tpl, self.test_password, action_name="密码输入框")
#
#             # 4. 勾选用户协议
#             self.check_and_click_optional(agree_tpl, action_name="用户协议勾选框")
#
#             # 5. 点击登录按钮
#             self.click_element(login_btn_tpl, action_name="登录按钮")
#
#             # 6. 验证登录结果
#             self.verify_login_success(success_tpl)
#
#             print("🎉 登录测试流程执行成功！")
#
#         except Exception as e:
#             print(f"❌ 登录测试失败: {str(e)}")
#             snapshot(msg="错误截图", filename=f"error_{int(time.time())}.png")  # 带时间戳的截图
#             raise
#         finally:
#             # 无论成功失败都关闭应用
#             self.stop_application()
#             print("📝 测试脚本执行结束")
#
#
# if __name__ == "__main__":
#     # 配置测试参数，注意模板路径要使用正确的字符串形式，加上 r 前缀表示原始字符串
#     TEST_CONFIG = {
#         "device_uri": "Android://127.0.0.1:5037/emulator-5554",  # 设备连接地址
#         "package_name": "com.kongfz.app",  # 应用包名，需替换为实际正确包名
#         "test_account": "",  # 替换为你的测试账号
#         "test_password": "123456Aa",    # 替换为你的测试密码
#         "templates": {
#             "my_tab": r"tpl1753705886665.png",         # "我的"标签页
#             "phone_input": r"tpl1753357948990.png",    # 手机号输入框模
#             "password_input": r"tpl1753357957613.png", # 密码输入框模
#             "agree_checkbox": r"tpl1753359784687.png", # 协议勾选框模
#             "login_button": r"tpl1753861792861.png",   # 登录按钮模板，
#             "success标志": r"tpl1753358025406.png"    # 登录成功验证模
#         }
#     }
#
#     # 初始化测试器并执行测试
#     tester = AppLoginTester(
#         device_uri=TEST_CONFIG["device_uri"],
#         package_name=TEST_CONFIG["package_name"],
#         test_account=TEST_CONFIG["test_account"],
#         test_password=TEST_CONFIG["test_password"]
#     )
#
#     # 执行登录测试
#     tester.run_login_test(
#         my_tab_tpl=TEST_CONFIG["templates"]["my_tab"],
#         phone_input_tpl=TEST_CONFIG["templates"]["phone_input"],
#         password_input_tpl=TEST_CONFIG["templates"]["password_input"],
#         agree_tpl=TEST_CONFIG["templates"]["agree_checkbox"],
#         login_btn_tpl=TEST_CONFIG["templates"]["login_button"],
#         success_tpl=TEST_CONFIG["templates"]["success标志"]
#     )
#
#
#
#
#
#
#
#
#
#